package cn.main.controller;

import cn.main.core.SessionHelper;
import cn.main.core.SessionKey;
import cn.main.core.UserSession;
import cn.main.service.SessionUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.net.www.http.HttpClient;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Author: chengwei
 * Date:   2016/7/14
 * Time:   14:27
 */
@Controller
@RequestMapping(value = "/login")
public class SessionUserController {
    
    @Resource
    private SessionUserService service;
    
    @RequestMapping
    public String login(){
        return "/login";
    }

    @RequestMapping(value = "/u")
    public String doLogin(String username, String password, @RequestParam(value = "check_code",required = false) String checkCode, HttpServletRequest request){
        boolean canLogin = service.doLogin(request,username,password,checkCode);
        if (canLogin) {
            return "redirect:/user/home";
        }
        request.getSession().removeAttribute(SessionKey.SESSION_KEY);
        return "redirect:/login";
    }
}
